package me.zxk.smartagriculture.repository;

import me.zxk.smartagriculture.entity.Note;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 便签数据访问层
 */
@Repository
public interface NoteRepository extends JpaRepository<Note, Long> {

    /**
     * 根据用户ID分页查询便签
     */
    Page<Note> findByUserId(Long userId, Pageable pageable);

    /**
     * 根据用户ID和便签ID查询便签
     */
    Optional<Note> findByIdAndUserId(Long id, Long userId);

    /**
     * 根据用户ID查询便签数量
     */
    long countByUserId(Long userId);

    /**
     * 根据用户ID和关键词搜索便签
     */
    @Query("SELECT n FROM Note n WHERE n.userId = :userId AND " +
           "(n.title LIKE %:keyword% OR n.content LIKE %:keyword%)")
    Page<Note> findByUserIdAndKeyword(@Param("userId") Long userId, 
                                     @Param("keyword") String keyword, 
                                     Pageable pageable);

    /**
     * 批量删除用户的便签
     */
    void deleteByUserIdAndIdIn(Long userId, List<Long> ids);

    /**
     * 删除用户的所有便签
     */
    void deleteByUserId(Long userId);
}
